package sbj.wizcore.common;

import com.alibaba.fastjson.JSON;
import sbj.wizcore.domain.WizDocument;

import java.sql.*;
import java.util.ArrayList;

public class SqlHelper {

    public static final String DOMAIN_USER = "hasee";


    public static Connection getConn() {
        String sql = String.format("jdbc:sqlite://C:/Users/%s/Documents/My Knowledge/Data/fanzhicheng_123@163.com/index.db", DOMAIN_USER);
        Connection conn = null;
        try {

            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection(sql);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }


    public static ResultSet exeQuery(String sql) {


        Connection conn = getConn();
        if (conn == null) {
            System.out.println("找不到数据库连接...");
            return null;
        }

        ResultSet rs = null;
        try {
            Statement stat = conn.createStatement();
            rs = stat.executeQuery(sql); // 查询数据
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }



    public static void main(String[] args) {

        getConn();

        // TODO Auto-generated method stub
        try {
            // 0 连接SQLite的JDBC
            String sql = String.format("jdbc:sqlite://C:\\Users\\%s\\Documents\\My Knowledge\\Data\\fanzhicheng_123@163.com\\index.db", DOMAIN_USER);
            Class.forName("org.sqlite.JDBC");

            // 1 建立一个数据库名zieckey.db的连接，如果不存在就在当前目录下创建之
            Connection conn = DriverManager.getConnection(sql);
            Statement stat = conn.createStatement();

            // 2 创建一个表tbl1，录入数据
            //stat.executeUpdate("drop table if exists tbl1;");
            //stat.executeUpdate("create table if not exists tbl1(name varchar(20), salary int);");// 创建一个表，两列
            //        stat.executeUpdate("insert into tbl1 values('ZhangSan',8000);"); // 插入数据
            //                 stat.executeUpdate("insert into tbl1 values('LiSi',7800);");
            //stat.executeUpdate("insert into tbl1 values('WangWu',5800);");
            // stat.executeUpdate("insert into tbl1 values('ZhaoLiu',9100);");


            ResultSet rs = stat.executeQuery("select * from WIZ_DOCUMENT;"); // 查询数据

            System.out.println("读取为知信息：");

            ArrayList<WizDocument> docArr = new ArrayList<WizDocument>();
            while (rs.next()) {
                WizDocument doc = new WizDocument();
                doc.setDocumentGuid(rs.getString("document_guid"));
                doc.setDocumentTitle(rs.getString("document_title"));
                doc.setDocumentLocation(rs.getString("document_title"));
                docArr.add(doc);
            }
            stat.close();
            rs.close();


            docArr.forEach((doc) -> {
                System.out.println(JSON.toJSONString(doc));
            });


            // 3 修改表结构，添加字段 address varchar(20) default 'changsha';
            // stat.executeUpdate("alter table tbl1 add column address varchar(20) not null default 'changsha'; ");// 创建一个表，两列
            //        stat.executeUpdate("insert into tbl1 values('HongQi',9000,'tianjing');"); // 插入数据
            //                stat.executeUpdate("insert into tbl1(name,salary) values('HongQi',9000);"); // 插入数据
            //                       rs = stat.executeQuery("select * from tbl1;"); // 查询数据
            //                               System.out.println("表结构变更操作演示：");
            // while (rs.next()) { // 将查询到的数据打印出来
            //    System.out.print("name = " + rs.getString("name") + ", "); // 列属性一
            //           System.out.print("name = " + rs.getString("name") + ", "); // 列属性二
            //                  System.out.println("address = " + rs.getString("address")); // 列属性三
            //}
            // rs.close();

            conn.close(); // 结束数据库的连接

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
